<?php
Zend_Loader::loadClass('Application_Form');
Zend_Loader::loadClass('Application_Form_Element_GlazingControls');

class Application_Form_VetriComposti extends Application_Form
{
	protected $_formDecorators = array(
			'FormElements',
			'Form',
			array('HtmlTag', array('tag' => 'div', 'id' => 'form-wrap')),
	);

	protected $_displaygroupDecorators = array(
			'FormElements',
			'Fieldset',
	);

	protected $_elementDecorators = array(
			'viewHelper',
			array(array('htmltag1' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-span')),
			array('Label', array('tag' => 'div', 'tagClass' => 'form-span'))
	);

	public function __construct() {
		parent::__construct();
		$this->addJavascript("/validator/jquery.validationEngine.js");
		$this->addJavascript("/validator/languages/jquery.validationEngine-en.js");
		$this->addJavascript("/dataTables/jquery.dataTables.min.js");
		$this->addStylesheet("/validator/validationEngine.jquery.css");
		$this->addStylesheet("/dataTables/jquery.dataTables.css");
		$this->addStylesheet("/dataTables/demo_table_jui.css");
		$this->addStylesheet("/dataTables/fix.css");
	}

	public function run() {
		$request = Zend_Controller_Front::getInstance()->getRequest();
		if (!$request->isXmlHttpRequest()) {
			// Contesto non-AJAX
			$this->_initForm();
			$this->_view->browserForm = $this->_form->getSubForm("browser")->render($this->_view);
			$this->_view->detailForm = $this->_form->getSubForm("detail")->render($this->_view);
			return;
		} else {
			// Contesto AJAX
			$layout = Zend_Layout::getMvcInstance();
			$layout->disableLayout();

			$ret = "";
			if ($request->isPost()) {
				// POST data
				foreach ($request->getParams() as $paramName => $paramValue) {
					$urlParams[$paramName] = urlencode($paramValue);
				}
				$this->_initForm();
				if ($this->_form->getSubForm("detail")->isValid($request->getParams())) {
					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
					$redirector->setGotoRoute($urlParams, 'ajax');
					$redirector->redirectAndExit();
				} else {
					$messages = $this->_form->getSubForm("detail")->getMessages();
					if (array_key_exists('glazing_name', $messages)) {
						array_unshift($messages['glazing_name'], "Corregere gli errori segnalati");
					} else {
						$messages['glazing_name'] = array ("generic" => "Corregere gli errori segnalati");
					}
					$ret = array("esito" => -1, "id" => $request->getParam('id'), "messages" => $messages);
				}
			} else {
				// Servizi ausiliari form
				switch ($request->getParam('function')) {
					case 'addGlazingPane':
						$id = $request->getParam('pane_order');
						$element = new Zend_Form_Element_Text("new_pane_{$id}_name");
						$element->setLabel("Strato " . ((int)$id - 9) . ":")
						->setOptions(array(
								"id" => "new_pane_{$id}_name",
								"order" => $id,
								"size" => 60,
								"class" => "idleField validate[required] newmat",
								"style" => "border-color: forestgreen"
								))
								->setRequired(true)
								->addDecorator(array("dyn" => 'HtmlTag'), array("tag" => "div", "class" => "dyn"));
						$ret .= $element->__toString();
						$element = new Zend_Form_Element_Text("new_pane_{$id}_thickness");
						$element->setLabel("Spessore dello strato " . ((int)$id - 9) . ":")
						->setOptions(array(
								"id" => "new_pane_{$id}_thickness",
								"order" => $id,
								"size" => 60,
								"class" => "idleField validate[required,custom[number]]",
								"style" => "border-color: forestgreen"
								))
								->setRequired(true)
								->addDecorator(array("dyn" => 'HtmlTag'), array("tag" => "div", "class" => "dyn"))
								->addValidator("Float", false, array("locale" => "en"));
						$ret .= $element->__toString();
						$element = new Zend_Form_Element_Hidden("new_pane_{$id}_id");
						$element->setOptions(array(
								"id" => "new_pane_{$id}_id",
								"order" => $id,
								"class" => "dyn"
								))
								->clearDecorators()
								->addDecorator('viewHelper');
						$ret .= $element->__toString();
						break;
					case 'addGlazingGas':
						$id = $request->getParam('pane_order');
						$element = new Zend_Form_Element_Text("new_gas_{$id}_name");
						$element->setLabel("Strato " . ((int)$id - 9) . ":")
						->setOptions(array(
								"id" => "new_gas_{$id}_name",
								"order" => $id,
								"size" => 60,
								"class" => "idleField validate[required] newmat",
								"style" => "border-color: forestgreen"
								))
								->setRequired(true)
								->addDecorator(array("dyn" => 'HtmlTag'), array("tag" => "div", "class" => "dyn"));
						$ret .= $element->__toString();
						$element = new Zend_Form_Element_Text("new_gas_{$id}_thickness");
						$element->setLabel("Spessore dello strato " . ((int)$id - 9) . ":")
						->setOptions(array(
								"id" => "new_gas_{$id}_thickness",
								"order" => $id,
								"size" => 60,
								"class" => "idleField validate[required,custom[number]]",
								"style" => "border-color: forestgreen"
								))
								->setRequired(true)
								->addDecorator(array("dyn" => 'HtmlTag'), array("tag" => "div", "class" => "dyn"))
								->addValidator("Float", false, array("locale" => "en"));
						$ret .= $element->__toString();
						$element = new Zend_Form_Element_Hidden("new_gas_{$id}_id");
						$element->setOptions(array(
								"id" => "new_gas_{$id}_id",
								"order" => $id,
								"class" => "dyn"
								))
								->clearDecorators()
								->addDecorator('viewHelper');
						$ret .= $element->__toString();
						break;
					default:
						$ret = null;
				}
			}
			echo $ret;
		}
	}

	protected function _initForm()
	{
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();

		$browserForm = new Zend_Form(array("id" => "browser"));
		$detailForm = new Zend_Form(array("id" => "detail"));
		$this->_form->addSubForm($browserForm, "browser");
		$this->_form->addSubForm($detailForm, "detail");

		///// TOP (dataTables) /////
		
		Zend_Loader::loadClass('Glazing');
		$vetricomposti = new Glazing();
		$recordSet = $vetricomposti->select()->from($vetricomposti, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->glazing = $vetricomposti->fetchAll($recordSet)->toArray();
		
		///// BOTTOM /////

		$campo1 = new Zend_Form_Element_Hidden('pane_order');
		$campo1->setValue(10)
		->setOptions(array(
				"id" => "pane_order"));

		$campo2 = new Zend_Form_Element_Text('glazing_name');
		$campo2->setLabel('Nome del Vetro Composto')
		->setOptions(array(
				"id" => "glazing_name",
				"order" => 3,
				"size" => 60,
				"class" => "validate[required]"
		))
		->setRequired(true);


		$campo3 = new Application_Form_Element_GlazingControls("glazing_controls");
		$campo3->setOptions(array("id" => "glazing_controls",
				"order" => 2));

		$campo4 = new Zend_Form_Element_Text('glazing_source');
		$campo4->setLabel('Fonte')
		->setOptions(array(
				"id" => "glazing_source",
				"order" => 4,
				"size" => 60));

		$campo5 = new Zend_Form_Element_Text('glazing_category');
		$campo5->setLabel('Categoria')
		->setOptions(array(
				"id" => "glazing_category",
				"order" => 5,
				"size" => 60));

		$campo6 = new Zend_Form_Element_Text('glazing_definition_method');
		$campo6->setLabel('Metodo di Definizione')
		->setOptions(array(
				"id" => "glazing_definition_method",
				"order" => 6,
				"size" => 60));
		
		$campo7 = new Zend_Form_Element_Text('glazing_layers');
		$campo7->setLabel('Numero strati')
		->setOptions(array(
				"id" => "glazing_layers",
				"order" => 7,
				"size" => 60));

		$campo8 = new Zend_Form_Element_Button('pane_add');
		$campo8->setLabel('Aggiungi struttura trasparente')
		->setOptions(array(
				"id" => "pane_add",
				"order" => 8,
				"style" => "margin-bottom: 20px"));
		
		$campo9 = new Zend_Form_Element_Button('gas_add');
		$campo9->setLabel('Aggiungi gas')
		->setOptions(array(
				"id" => "gas_add",
				"order" => 9,
				"style" => "margin-bottom: 20px"));
		
		$campo10 = new Zend_Form_Element_Hidden('glazing_category_id');
		$campo10->setOptions(array(
				"id" => "glazing_category_id",
		));

		// campi dinamici

		$campo100 = new Zend_Form_Element_Text('glazing_transsolar');
		$campo100->setLabel('Trasmissione solare totale (SHGC)')
		->setOptions(array(
				"id" => "glazing_transsolar",
				"order" => 100,
				"size" => 60,
				"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$campo101 = new Zend_Form_Element_Text('glazing_transdirectsolar');
		$campo101->setLabel('Trasmissione solare diretta')
		->setOptions(array(
				"id" => "glazing_transdirectsolar",
				"order" => 101,
				"size" => 60,
				"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$campo102 = new Zend_Form_Element_Text('glazing_translight');
		$campo102->setLabel('Trasmissione luce')
		->setOptions(array(
				"id" => "glazing_translight",
				"order" => 102,
				"size" => 60,
				"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$campo103 = new Zend_Form_Element_Text('glazing_uvalue');
		$campo103->setLabel('Valore U (W/m^2 K)')
		->setOptions(array(
				"id" => "glazing_uvalue",
				"order" => 103,
				"size" => 60,
				"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		
		$campo104 = new Zend_Form_Element_Text('glazing_en673uvalue');
		$campo104->setLabel('Valore U (EN 673) (W/m^2 K)')
		->setOptions(array(
				"id" => "glazing_en673uvalue",
				"order" => 106,
				"size" => 60,
				"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$this->_form->getSubForm("detail")->addElement($campo1);
		$this->_form->getSubForm("detail")->addElement($campo2);
		$this->_form->getSubForm("detail")->addElement($campo3);
		$this->_form->getSubForm("detail")->addElement($campo4);
		$this->_form->getSubForm("detail")->addElement($campo5);
		$this->_form->getSubForm("detail")->addElement($campo6);
		$this->_form->getSubForm("detail")->addElement($campo7);
		$this->_form->getSubForm("detail")->addElement($campo8);
		$this->_form->getSubForm("detail")->addElement($campo9);
		$this->_form->getSubForm("detail")->addElement($campo10);
		$this->_form->getSubForm("detail")->addElement($campo100);
		$this->_form->getSubForm("detail")->addElement($campo101);
		$this->_form->getSubForm("detail")->addElement($campo102);
		$this->_form->getSubForm("detail")->addElement($campo103);
		$this->_form->getSubForm("detail")->addElement($campo104);
		
		// Elementi hidden non devono avere decorators
		$elements = $this->_form->getSubForm("detail")->getElements();
		foreach ($elements as $elem) {
			if ($elem instanceof Zend_Form_Element_Hidden)
				$elem->setDecorators(array('ViewHelper'));
		}
		
		// Datasources
		Zend_Loader::loadClass('GlazingCategories');
		$categorie = new GlazingCategories();
		$recordSet = $categorie->select()->from($categorie, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->glazing_categories = $categorie->fetchAll($recordSet)->toArray();
	
	}
}
